package com.recSystem.Config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// 拦截器
public class LoginHandlerInterceptor  implements HandlerInterceptor {

    private static final Logger log= LoggerFactory.getLogger(LoginHandlerInterceptor.class);
    @Value("${frontEnd.path}")
    private static String frontEndPath;

    // 这里只判断头部是否有token
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        long startTime = System.currentTimeMillis();

        log.debug("拦截器");
        String token = request.getHeader("Authorization");
        if(token == null) {
            log.debug("============没带Authorization头部");
            // TODO 这路径不对
//            response.sendRedirect(request.getContextPath() + "/login");
            response.sendRedirect(frontEndPath+"/login");
            return false;
        }
        System.out.println("token存在");
        return true;
    }
}
